﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace NRemedy.MVC.UI
{
    [AttributeUsage(AttributeTargets.Method,AllowMultiple=false,Inherited=true)]
    public class JsonExceptionHandlerAttribute : FilterAttribute, IExceptionFilter
    {
        public void OnException(System.Web.Mvc.ExceptionContext filterContext)
        {
            JosnErrorResponse response = new JosnErrorResponse();
            response.Exception = filterContext.Exception.ToString();
            filterContext.ExceptionHandled = true;
            //filterContext.Result = ((Controller)filterContext.Controller).Json(response);
            JsonBuilder builder = new JsonBuilder();
            builder.MergeProperty("Exception", response.Exception);
            filterContext.Result = new RawJsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = builder.ToString() };

        }
    }
}
